package com.leet.code.simple_array;

import java.util.HashMap;
import java.util.Map;

/**
 * @description:
 * @author: WYG
 * @time: 2020/11/30 15:27
 */

public class HasGroupsSizeX {

    public boolean otherHasGroupsSizeX(int[] deck) {
        int[] counter = new int[10000];
        for (int i = 0; i < deck.length; i++) {
            counter[deck[i]]++;
        }
        int t = 0;
        for (int x : counter) {
            t = gcd(t, x);
            if (t == 1) {
                return false;
            }
        }
        return t > 1;
    }

    public boolean hasGroupsSizeX(int[] deck) {
        Map<Integer, Integer> elements = new HashMap<Integer, Integer>();
        for (int i = 0; i < deck.length; i++) {
            elements.put(deck[i], elements.getOrDefault(deck[i], 0) + 1);
        }
        int t = 0;
        for (Integer x : elements.keySet()) {
            t = gcd(t, elements.get(x));
            if (t == 1) {
                return false;
            }
        }
        return t > 1;
    }
    public int gcd(int a, int b) {
        return b == 0 ? a : gcd(b, a % b);
    }

}
